//
//  Settings_VC.m
//  Typical memes
//
//  Created by Denis Demjanko on 01.07.12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "About_VC.h"

@implementation About_VC


#pragma mark - View lifecycle

- (void)setNavTitle:(NSString *)title
{
    UILabel *titleView = (UILabel *)self.navigationItem.titleView;
    if (!titleView) {
        titleView = [[UILabel alloc] initWithFrame:CGRectZero];
        titleView.backgroundColor = [UIColor clearColor];
        titleView.font = [UIFont boldSystemFontOfSize:20.0];
        titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
        
        titleView.textColor = [UIColor colorWithRed:187.0f/255.0f green:187.0f/255.0f blue:187.0f/255.0f alpha:1.0f]; // Change to desired color
        
        self.navigationItem.titleView = titleView;
    }
    
    titleView.text = title;
    [titleView sizeToFit];
}
- (void)viewDidLoad{
    [super viewDidLoad];
    
    [self setNavTitle:@"Инфо"];

    scroller = [[MGScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    scroller.alwaysBounceVertical = YES;
    scroller.delegate = self;
    [self.view addSubview:scroller];

    [self addBoxes];
}

-(void)addBoxes{    

    
    MGStyledBox *box_2 = [MGStyledBox box];
    
    MGBoxLine *header_box_2 = [MGBoxLine multilineWithText:@"Понравилось приложения? Проголосуйте за нас!" font:[UIFont fontWithName:@"HelveticaNeue-Bold" size:16] padding:24.0f];
    [box_2.topLines addObject:header_box_2];
    
    UIButton *star = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 36, 36)];
    [star setBackgroundImage:[UIImage imageNamed:@"star_gold.png"] forState:UIControlStateNormal];
    [star setBackgroundImage:[UIImage imageNamed:@"star_gold_1.png"] forState:UIControlStateHighlighted];
    [star addTarget:self action:@selector(onRate) forControlEvents:UIControlEventTouchUpInside];
    
    MGBoxLine *line_box_3 = [MGBoxLine lineWithLeft:@"Проголосовать" right:star];
    [box_2.topLines addObject:line_box_3];
    
    MGStyledBox *box_3 = [MGStyledBox box];
    
    MGBoxLine *header_box_3 = [MGBoxLine multilineWithText:@"У Вас появились вопросы или пожелания? Напишите нам!" font:[UIFont fontWithName:@"HelveticaNeue-Bold" size:16] padding:24.0f];
    [box_3.topLines addObject:header_box_3];
    
    UIButton *mail = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
    [mail setBackgroundImage:[UIImage imageNamed:@"m_icon.png"] forState:UIControlStateNormal];
    [mail setBackgroundImage:[UIImage imageNamed:@"m_icon2.png"] forState:UIControlStateHighlighted];
    [mail addTarget:self action:@selector(onMail) forControlEvents:UIControlEventTouchUpInside];
    
    MGBoxLine *line_box_4 = [MGBoxLine lineWithLeft:@"Написать письмо" right:mail];
    [box_3.topLines addObject:line_box_4];
    
    
    UIView *login_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 88, 35)];
    
    UIButton *vk_button = [UIButton buttonWithType:UIButtonTypeCustom];
    [vk_button setFrame:CGRectMake(0, 0, 35, 35)];
    [vk_button setBackgroundImage:[UIImage imageNamed:@"vk-icon-login"] forState:UIControlStateNormal];
    [vk_button addTarget:self action:@selector(vkonatkte) forControlEvents:UIControlEventTouchUpInside];
    [login_view addSubview:vk_button];
    
    UIButton *fb_button = [UIButton buttonWithType:UIButtonTypeCustom];
    [fb_button setFrame:CGRectMake(50, 0, 35, 35)];
    [fb_button setBackgroundImage:[UIImage imageNamed:@"facebook-icon-login"] forState:UIControlStateNormal];
    [fb_button addTarget:self action:@selector(facebook) forControlEvents:UIControlEventTouchUpInside];
    [login_view addSubview:fb_button];
    
    MGStyledBox *box_4 = [MGStyledBox box];

    MGBoxLine *line_box = [MGBoxLine lineWithLeft:@"Мы в социальных сетях" right:login_view];
    line_box.tag = 12;
    [box_4.topLines addObject:line_box];
    
    
    [scroller.boxes addObject:box_2];
    [scroller.boxes addObject:box_3];
    [scroller.boxes addObject:box_4];

    [scroller drawBoxesWithSpeed:0.0f];
    [scroller flashScrollIndicators];
}

#pragma mark - IBActions

-(void)vkonatkte{
	[[UIApplication sharedApplication] openURL: [NSURL URLWithString: @"http://vkontakte.ru/club32955452"]];
}	


-(void)facebook{
	[[UIApplication sharedApplication] openURL: [NSURL URLWithString: @"http://www.facebook.com/iWorldOfFacts"]];
}

-(void)onRate{
	NSString* url = [NSString stringWithFormat:
					 @"http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=%@&pageNumber=0&sortOrdering=1&type=Purple+Software&mt=8", @"488108471"];
	[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
}

-(void)onMail{
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
	if (mailClass != nil){
		if ([mailClass canSendMail]){
			[self displayComposerSheet];
		}
		else{
			[self launchMailAppOnDevice];
		}
	}
	else{
		[self launchMailAppOnDevice];
	}
 
}

#pragma mark - UIAlertView delegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

}


#pragma mark - UIScrollViewDelegate (for snapping boxes to edges)

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [(MGScrollView *)scrollView snapToNearestBox];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView  willDecelerate:(BOOL)decelerate {
    if (!decelerate) 
        [(MGScrollView *)scrollView snapToNearestBox];
}


#pragma mark Compose Mail

-(void)displayComposerSheet {
	MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
	picker.mailComposeDelegate = self;
	[picker setSubject:@"Мир фактов - iPhone Application"];
	
	NSArray *toRecipients = [NSArray arrayWithObject:@"denis.d@it-dimension.com"]; 
	[picker setToRecipients:toRecipients];
	
	[self presentModalViewController:picker animated:YES];
}


- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {	
	[self dismissModalViewControllerAnimated:YES];
}


-(void)launchMailAppOnDevice{
	NSString *recipients = @"mailto:denis.d@it-dimension.com&subject=Мир фактов - iPhone Application";
	
	NSString *email = [NSString stringWithFormat:@"%@", recipients];
	email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	
	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}

#pragma mark - Memory Management

- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}



@end
